package cn.com.hori.cms.service.impl;

import cn.com.hori.cms.dao.ColumnRepository;
import cn.com.hori.cms.entities.ColumnAdmin;
import cn.com.hori.cms.model.ColumnTreeNode;
import cn.com.hori.cms.service.ColumnService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘艺
 * @date 2018/3/12 10:29
 */
@Service
public class ColumnServiceImpl implements ColumnService {

    /**
     * 打日志
     */
    private static Logger logger = LoggerFactory.getLogger(ColumnServiceImpl.class);

    /**
     * 栏目 仓库
     */
    @Autowired
    private ColumnRepository columnRepository;

    /**
     * 根据栏目ID 查询栏目的详细信息
     *
     * @param columnId 被查询的的栏目ID
     * @return 返回栏目实体
     */
    @Override
    public ColumnAdmin getColumn(String columnId) {
        return columnRepository.getOneBycolumnId(columnId);

    }

    /**
     * 保存 或 更新 栏目信息
     *
     * @param columnAdmin 被保存 或 更新 的栏目实体
     * @return 返回被保存 或更新的栏目实体
     */
    @Override
    public ColumnAdmin updateColumn(ColumnAdmin columnAdmin) {
        if (columnAdmin.getId() == null) {
            ColumnAdmin temp = columnRepository.getOneBycolumnId(columnAdmin.getColumnId());
            if (temp != null) {
                columnAdmin.setId(temp.getId());
            }

        }
        return columnRepository.save(columnAdmin);
    }

    /**
     * 删除栏目信息
     *
     * @param columnAdmin 被删除的栏目实体
     * @return 成功删除返回 true ，删除失败返回 false
     */
    @Override
    public String deleteColumn(ColumnAdmin columnAdmin) {
        if (columnAdmin.getId() == null) {
            ColumnAdmin temp = columnRepository.getOneBycolumnId(columnAdmin.getColumnId());
            if (temp != null) {
                columnAdmin.setId(temp.getId());
            } else {
                logger.info("数据库中不存在该被删除的值：{}", columnAdmin);
                return "操作数据已被删除！";
            }

        }
        // 存在子节点，不能删除
        List<ColumnAdmin> tempList = columnRepository.getAllBySuperiorColumnIdEquals(columnAdmin.getColumnId());
        if (tempList != null && tempList.size() >0){
            return "存在子节点，不能删除！";
        }

        try {
            columnRepository.delete(columnAdmin);
            return "success";
        } catch (Exception e) {
            logger.info("数据库操作失败:{}", e);
            return "数据库写入失败！";
        }
    }


    /**
     * 获得栏目树
     *
     * @return 返回栏目树 数据结构
     */
    @Override
    public ColumnTreeNode getTree() {
        ColumnTreeNode rootNode = new ColumnTreeNode();
        ColumnAdmin rootColumn = columnRepository.getOneBycolumnId("-1");
        if (rootColumn != null) {
            rootNode.setColumnId(rootColumn.getColumnId());
            rootNode.setColumnName(rootColumn.getColumnName());
            rootNode.setSubtree(null);
            getListTree(rootNode);
        }

        return rootNode;
    }


    /**
     * 构造栏目树
     *
     * @param node 根节点
     */
    private void getListTree(ColumnTreeNode node) {
        List children = node.getSubtree();
        if (children == null) {
            List<ColumnAdmin> organizationList = columnRepository.getAllBySuperiorColumnIdEquals(node.getColumnId());
            if (organizationList.size() != 0) {
                node.setSubtree(new ArrayList<>());
            }
            for (ColumnAdmin columnAdmin : organizationList) {
                ColumnTreeNode node1 = new ColumnTreeNode();
                node1.setColumnId(columnAdmin.getColumnId());
                node1.setColumnName(columnAdmin.getColumnName());
                node1.setSubtree(null);
                node.getSubtree().add(node1);
                getListTree(node1);
            }
        }
    }
}
